package net.wanho.shiro.service;

import net.wanho.po.sys.User;
import net.wanho.shiro.constant.ShiroConstants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class LogoutService {

    @Autowired
    private RedisTemplate redisTemplate;

    public  void deleteSessionAndCache(){
        Subject subject = SecurityUtils.getSubject();
        User user  = (User) subject.getPrincipal();
        Session session = subject.getSession();

        Serializable sessionKey = session.getId();
        String cacheKey = ShiroConstants.SHIRO_CACHE_PREFIX+user.getUserId();
        // 从redis中删除当前人的session
        redisTemplate.delete(sessionKey);
        // 从redis中删除当前人的cache
        redisTemplate.delete(cacheKey);
    }
}
